<?php
if($_SESSION['group_id']!=2){redir("./");exit;}

if($_GET['op']==''){
?>
<div class="box">
  <h3>รายวิชาของท่าน</h3>
  <div>
  	<form method="get">
		<input name="search" value="<?php echo $search;?>" />
		<input type="hidden" name="mod" value="<?php echo $mod;?>" />
		<input type="submit" value="ค้นหา" />
	</form>
  </div>
  <div class="boxContent">
  <?php
  $where=" where m_id=$_SESSION[m_id] and course_name like '%$search%'";
  $sql="select * from course $where order by course_name asc";
  $res=mysql_query($sql) or die(mysql_error());
  $page=$_GET['page'];
  if(!$page)$page=1;
  $allrows=mysql_num_rows($res);
  $allpage=ceil($allrows/$cfg_limit);
  $limitstart=($page-1)*$cfg_limit;
  $sql.=" limit $limitstart,$cfg_limit";
  $res=mysql_query($sql);
  $pageurl="?mod=$mod&search=$search";
  if($allrows>0){
  ?>
    <form method="post">
	<table width="100%">
      <tr>
        <td width="10"><strong>#</strong></td>
        <td><strong>ชื่อวิชา</strong></td>
        <td colspan="4" align="center">&nbsp;</td>
        <td colspan="2" align="center"><strong>ดำเนินการ</strong></td>
	  </tr>
	  <?php
	  	$res=mysql_query($sql);
		$i=0;
	  	while($row=mysql_fetch_assoc($res)){
			++$i;
	  		$class=($i%2!=0)?'rowA':'rowB';
	  ?>
      <tr class="<?php echo $class;?>">
        <td width="10"><?php echo $i;?></td>
        <td><?php echo $row['course_name'];?></td>
        <td align="center"><?php echo "<a href=?mod=member&redir=".urlencode("?mod=t.liststudent&course_id=$row[course_id]").">รายชื่อนักศึกษา</a>";?></td>
        <td align="center"><?php echo "<a href=?mod=t.listlesson&course_id=$row[course_id]>บทเรียน</a>";?></td>
        <td align="center"><?php echo "<a href=?mod=member&redir=".urlencode("t.viewscore.php?&course_id=$row[course_id]")." target=_blank>คะแนนสอบ</a>";?></td>
        <td align="center"><?php echo "<a href=t.viewallscore.php?&course_id=$row[course_id] target=_blank>คะแนนสอบรวม</a>";?></td>
        <td align="center"><?php echo "<a href=?mod=$mod&op=edit&course_id=$row[course_id]>แก้ไข</a>";?></td>
        <td align="center"><?php echo "<a href=?mod=$mod&op=del&course_id=$row[course_id] onclick=\"return confirm('ยืนยันการลบ');\">ลบ</a>";?></td>
        </tr>
	  <?php
	  }
	  ?>
    </table>
	</form>
  <?php
  	pagemenu($pageurl,$page,$allpage);
  }else{
  	echo "<div id=error_msg>ไม่พบข้อมูล</div>";
  }
  ?>
  </div>
</div>
<?php
}else if($_GET['op']=='edit'){
	if($_POST['op']=='update'){
		$course_name=$_POST['course_name'];
		if(!$course_name)$err="กรุณากรอก ชื่อวิชา";
		else{
			$sql="update course set course_name='$course_name' where course_id='$_GET[course_id]'";
			mysql_query($sql);
			redir("?mod=$mod",5);
			die("<div id=com_msg>ปรับปรุงข้อมูลเรียบร้อยแล้วกรุณารอสักครู่ <img src=images/load.gif></div>");
		}
	}
	$row=mysql_fetch_assoc(mysql_query("select * from course where course_id='$_GET[course_id]'"));
?>
<div class="box">
  <h2>แก้ไขข้อมูลรายวิชา</h2>
  <?php if($err)echo "<div id=error_msg>$err</div>";?>
  <div class="boxContent">
    <p> </p>
    <form method="post" enctype="multipart/form-data">
      <table width="99%" border="0" cellspacing="2" cellpadding="2">
        <tr>
          <td width="120"><strong>ชื่อวิชา</strong></td>
          <td><input name="course_name" type="text" id="course_name" value="<?php echo $row['course_name'];?>" /></td>
        </tr>
      </table>
      <table width="99%" border="0" cellspacing="2" cellpadding="2">
        <tr>
          <td align="center"><input type="submit" class="button" name="Submit" value="ปรับปรุงข้อมูล" />
              <input name="op" type="hidden" id="op" value="update" /></td>
        </tr>
      </table>
    </form>
    </p>
  </div>
</div>
<?php
}else if($_GET['op']=='del'){
	mysql_query("delete from course where course_id='$_GET[course_id]'");
	redir("?mod=$mod&".time());exit;
}
?>